<template>
<div>
  <!--    返回导航栏-->
  <van-nav-bar
    title="注册"
    left-text="返回"
    left-arrow
    @click-left="onClickLeft"
  />
  <van-form @submit="onSubmit" class="login">
    <van-cell-group inset>
      <van-field
        v-model="ruleForm.name"
        name="name"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="ruleForm.password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />

    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>
</div>
</template>

<script>
export default {
  name: "register",

  data(){
    return{
      ruleForm: {
        name:'',
        password:'',

      }

    }
  },
  methods:{
    onSubmit(values){
      console.log('submit', values);
      let _this=this;
      this.$axios.post('user/registerUser',this.ruleForm).then(
        (resp) => {
          if (resp.data.result) {
            this.$toast.success('注册成功');
            _this.$router.push("/login");
          } else {
            this.$toast.fail('注册失败');
          }
        }
      ).catch(
          function(error){

              console.log(error)

          }
      );

    },
    onClickLeft(){
      this.$router.back()
    }
  }
}
</script>

<style scoped>
.login{
  margin: auto;
  width: 6rem;
  margin-top: 30vh;
}
</style>
